#coding=utf-8

from handlers import *


class PageHandler(BaseHandler, PageMixin):
	def get(self, slug):
		page = self.get_page(slug)
		
		if not page:
			permalink = self.get_permalink(slug)
			if permalink:
				self.redirect_to('page', permalink)
			
			raise tornado.web.HTTPError(404)
		
		if page.hidden:
			raise tornado.web.HTTPError(404)
		
		self.render('page.html', contents=page)
		
		
class FinanceHandler(BaseHandler):
	def get(self):
		self.render('finance.html', data=None)
		
		
class AddFinanceHandler(BaseHandler):
	def get(self):
		self.render('add_finance.html', data=None)
		
		
class BaseCategoryHandler(BaseHandler):
	def get(self):
		data = self.db.query('SELECT * FROM base_category ORDER BY MODIFIED DESC')
		self.render('base_category.html', data=data)
		
			
class AddBaseCategoryHandler(BaseHandler):
	def get(self):
		self.render('add_base_category.html')
		
	def post(self):
		categoryCode = self.get_argument('categoryCode')
		categoryName = self.get_argument('categoryName')
		category = self.db.query('SELECT * FROM base_category WHERE CATEGORY_CODE = %s', categoryCode)
		if not category:
			self.db.execute("INSERT INTO base_category(CATEGORY_CODE, CATEGORY_NAME, CREATED) VALUES(%s, %s, now())", categoryCode, categoryName)
			self.redirect_to('base_category')
			
			
class DeleteBaseCategoryHandler(BaseHandler):
	def get(self):
		categoryId = self.get_argument('categoryId')
		self.db.execute('DELETE FROM base_category WHERE ID = %s', categoryId)
		self.redirect_to('base_category')
		
		
class BaseItemHandler(BaseHandler):
	def get(self):
		data = self.db.query('SELECT * FROM base_item ORDER BY MODIFIED DESC')
		self.render('base_item.html', data=data)
		
		
class AddBaseItemHandler(BaseHandler):
	def get(self):
		data = self.db.query('SELECT * FROM base_category ORDER BY MODIFIED DESC')
		self.render('add_base_item.html', data=data)
		
	def post(self):
		categoryId = self.get_argument('categoryId')
		itemKey = self.get_argument('itemKey')
		itemValue = self.get_argument('itemValue')
		item = self.db.query('SELECT * FROM base_item WHERE CATEGORY_ID = %s AND ITEM_KEY = %s', categoryId, itemKey)
		if not item:
			self.db.execute("INSERT INTO base_item(CATEGORY_ID, ITEM_KEY, ITEM_VALUE, CREATED) VALUES(%s, %s, %s, now())", categoryId, itemKey, itemValue)
			self.redirect_to('base_item')
			
			
class DeleteBaseItemHandler(BaseHandler):
	def get(self):
		itemId = self.get_argument('itemId')
		self.db.execute('DELETE FROM base_item WHERE ID = %s', itemId)
		self.redirect_to('base_item')
		
		
class LineHandler(BaseHandler):
	def get(self):
		data = self.db.query('SELECT * FROM line ORDER BY MODIFIED DESC')
		self.render('line.html', data=data)
		
			
class AddLineHandler(BaseHandler):
	def get(self):
		self.render('add_line.html')
		
	def post(self):
		lineCode = self.get_argument('lineCode')
		lineName = self.get_argument('lineName')
		linePrice = self.get_argument('linePrice')
		line = self.db.query('SELECT * FROM line WHERE LINE_CODE = %s', lineCode)
		if not line:
			self.db.execute("INSERT INTO line(LINE_CODE, LINE_NAME, LINE_PRICE, CREATED) VALUES(%s, %s, %s, now())", lineCode, lineName, linePrice)
			self.redirect_to('line')
			
			
class DeleteLineHandler(BaseHandler):
	def get(self):
		lineId = self.get_argument('lineId')
		self.db.execute('DELETE FROM line WHERE ID = %s', lineId)
		self.redirect_to('line')